package pub.developers.forum.facade.impl;

import org.springframework.stereotype.Service;
import pub.developers.forum.api.model.PageRequestModel;
import pub.developers.forum.api.model.PageResponseModel;
import pub.developers.forum.api.model.ResultModel;
import pub.developers.forum.api.request.comment.CommentCreateRequest;
import pub.developers.forum.api.response.comment.CommentPageResponse;
import pub.developers.forum.api.service.CommentApiService;
import pub.developers.forum.app.manager.CommentManager;
import pub.developers.forum.common.enums.ErrorCodeEn;
import pub.developers.forum.domain.service.OssTxtService;
import pub.developers.forum.facade.support.ResultModelUtil;
import pub.developers.forum.facade.validator.CommentValidator;
import pub.developers.forum.facade.validator.PageRequestModelValidator;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class CommentApiServiceImpl implements CommentApiService {

    @Resource
    private CommentManager commentManager;

    @Resource
    private OssTxtService ossTxtService;

    @Override
    public ResultModel create(CommentCreateRequest request) throws Exception {
        CommentValidator.create(request);

        //评论内容审核
        List<Map<String,String>> result = ossTxtService.checkText(request.getContent());
        //////System.out.println("##" + result);
        if ( result.get(0).get("suggestion").equals("block") ) {
            return ResultModelUtil.fail(ErrorCodeEn.TXT_IS_NOPASS);
        }
        commentManager.create(request);

        return ResultModelUtil.success();
    }

    @Override
    public ResultModel<PageResponseModel<CommentPageResponse>> page(PageRequestModel<Long> pageRequest) {
        PageRequestModelValidator.validator(pageRequest);

        return ResultModelUtil.success(commentManager.page(pageRequest));
    }

    public ResultModel<List<CommentPageResponse>> beCommented (){
        return ResultModelUtil.success(commentManager.beCommented());
    }

    @Override
    public ResultModel<List<CommentPageResponse>> getListByPostId(Long postId) {
        return ResultModelUtil.success(commentManager.getListByPostId(postId));
    }

    //给评论点赞的Service层
    @Override
    public ResultModel followCommentService(Long commentID)
    {
        commentManager.followCommentManager(commentID);
        return ResultModelUtil.success();
    }

    //给评论取消点赞的Service层
    @Override
    public ResultModel unfollowCommentService(Long commentID) {
        commentManager.unfollowCommentManager(commentID);
        return ResultModelUtil.success();
    }



    @Override
    public ResultModel<PageResponseModel<CommentPageResponse>> allReplyService(PageRequestModel pageRequestModel) {
        // ResultModelUtil.success(commentManager.page(pageRequest))
        return ResultModelUtil.success(commentManager.replyPage(pageRequestModel));
    }
}
